package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    @Override
    public void addCart(ShoppingCartDTO dto) {
        //创建shoppingCart对象
        ShoppingCart shoppingCart = new ShoppingCart();
        //拷贝属性值
        BeanUtils.copyProperties(dto,shoppingCart);

        //1查询当前用户购物车中是否已添加该商品--条件dish_id,dish_flavor,user_id
        shoppingCart.setUserId(BaseContext.getCurrentId());
        ShoppingCart cart = shoppingCartMapper.selectBy(shoppingCart);
        if (cart == null){ //购物车中没有该商品
            //2补充缺失的属性值
            //判断是新增套餐还是新增菜品
            if (dto.getDishId() != null){   //代表添加的是菜品
                //根据菜品id查询菜品数据
                Dish dish = dishMapper.selectById(dto.getDishId());
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());
            }
        else {//代表添加的是套餐
            Setmeal setmeal = setmealMapper.getById(dto.getSetmealId());
            shoppingCart.setName(setmeal.getName());
            shoppingCart.setImage(setmeal.getImage());
            shoppingCart.setAmount(setmeal.getPrice());
        }
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            //插入数据
            shoppingCartMapper.insert(shoppingCart);
    } else{ //购物车中已添加该商品
            //将原来的购物车商品数量+1，调用mapper更新方法
            cart.setNumber(cart.getNumber() + 1);
            shoppingCartMapper.update(cart);
    }
}
    @Override
    public List<ShoppingCart> list() {
        //注意：查询条件为userId，只能看自己名下的购物车
        return shoppingCartMapper.list(BaseContext.getCurrentId());
    }

    @Override
    public void clean() {
        //删除当前用户购物车数据
        shoppingCartMapper.delete(BaseContext.getCurrentId());
    }

    @Override
    public void sub(ShoppingCartDTO dto) {
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(BaseContext.getCurrentId());
        shoppingCart.setDishId(dto.getDishId());
        shoppingCart.setSetmealId(dto.getSetmealId());
        ShoppingCart cart = shoppingCartMapper.selectBy(shoppingCart);
        if (cart != null && cart.getNumber() > 1){
            cart.setNumber(cart.getNumber() - 1);
        }
        shoppingCartMapper.update(cart);
        assert cart != null;
        if (cart.getNumber() == 0){
            shoppingCartMapper.deleteById(shoppingCart.getId());
        }
    }
}
